Completed
Push — master ( 2177e8...5b06fe )
by Felipe
12s
created

WebFXLoadTree.createItemFromElement   F

Complexity

Conditions 16
Paths 2304

Size

Total Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
nc 2304
nop 1
dl 0
loc 83
rs 2.0134
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like WebFXLoadTree.createItemFromElement often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*----------------------------------------------------------------------------\
2
|                        XLoadTree 2 PRE RELEASE                              |
3
|                                                                             |
4
| This is a pre release and may not be redistributed.                         |
5
| Watch http://webfx.eae.net for the final version                            |
6
|                                                                             |
7
|-----------------------------------------------------------------------------|
8
|                   Created by Erik Arvidsson & Emil A Eklund                 |
9
|                  (http://webfx.eae.net/contact.html#erik)                   |
10
|                  (http://webfx.eae.net/contact.html#emil)                   |
11
|                      For WebFX (http://webfx.eae.net/)                      |
12
|-----------------------------------------------------------------------------|
13
| A tree menu system for IE 5.5+, Mozilla 1.4+, Opera 7.5+                    |
14
|-----------------------------------------------------------------------------|
15
|         Copyright (c) 1999 - 2005 Erik Arvidsson & Emil A Eklund            |
16
|-----------------------------------------------------------------------------|
17
| This software is provided "as is", without warranty of any kind, express or |
18
| implied, including  but not limited  to the warranties of  merchantability, |
19
| fitness for a particular purpose and noninfringement. In no event shall the |
20
| authors or  copyright  holders be  liable for any claim,  damages or  other |
21
| liability, whether  in an  action of  contract, tort  or otherwise, arising |
22
| from,  out of  or in  connection with  the software or  the  use  or  other |
23
| dealings in the software.                                                   |
24
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
25
| This  software is  available under the  three different licenses  mentioned |
26
| below.  To use this software you must chose, and qualify, for one of those. |
27
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
28
| The WebFX Non-Commercial License          http://webfx.eae.net/license.html |
29
| Permits  anyone the right to use the  software in a  non-commercial context |
30
| free of charge.                                                             |
31
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
32
| The WebFX Commercial license           http://webfx.eae.net/commercial.html |
33
| Permits the  license holder the right to use  the software in a  commercial |
34
| context. Such license must be specifically obtained, however it's valid for |
35
| any number of  implementations of the licensed software.                    |
36
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
37
| GPL - The GNU General Public License    http://www.gnu.org/licenses/gpl.txt |
38
| Permits anyone the right to use and modify the software without limitations |
39
| as long as proper  credits are given  and the original  and modified source |
40
| code are included. Requires  that the final product, software derivate from |
41
| the original  source or any  software  utilizing a GPL  component, such  as |
42
| this, is also licensed under the GPL license.                               |
43
|-----------------------------------------------------------------------------|
44
| 2004-02-21 | Pre release distributed to a few selected tester               |
45
| 2005-06-06 | Removed dependency on XML Extras                               |
46
|-----------------------------------------------------------------------------|
47
| Dependencies: xtree2.js Supplies the tree control                           |
48
|-----------------------------------------------------------------------------|
49
| Created 2003-??-?? | All changes are in the log above. | Updated 2004-06-06 |
50
|-----------------------------------------------------------------------------|
51
| Note local changes have been made to allow Icons to have different links    |
52
|  than thier text label counterparts. Thanks to JGuillaume de Rorthais       |
53
\----------------------------------------------------------------------------*/
54
55
webFXTreeConfig.loadingText = "Loading...";
56
webFXTreeConfig.loadingIcon = "images/loading.gif";
57
webFXTreeConfig.errorLoadingText = "Error Loading";
58
webFXTreeConfig.errorIcon = "images/exclamation.16.png";
59
webFXTreeConfig.reloadText = "Click to reload";
60
61
62
function WebFXLoadTree(sText, sXmlSrc, oAction, sBehavior, sIcon, oIconAction, sOpenIcon) {
63
	WebFXTree.call(this, sText, oAction, sBehavior, sIcon, oIconAction, sOpenIcon);
64
65
	// setup default property values
66
	this.src = sXmlSrc;
67
	this.loading = !sXmlSrc;
68
	this.loaded = !sXmlSrc;
69
	this.errorText = "";
70
71
	if (this.src) {
72
		/// add loading Item
73
		this._loadingItem = WebFXLoadTree.createLoadingItem();
74
		this.add(this._loadingItem);
75
76
		if (this.getExpanded()) {
77
			WebFXLoadTree.loadXmlDocument(this);
78
		}
79
	}
80
}
81
82
WebFXLoadTree.createLoadingItem = function () {
83
	return new WebFXTreeItem(webFXTreeConfig.loadingText, null, null,
84
		webFXTreeConfig.loadingIcon);
85
};
86
87
_p = WebFXLoadTree.prototype = new WebFXTree;
88
89
_p.setExpanded = function (b) {
90
	WebFXTree.prototype.setExpanded.call(this, b);
91
92
	if (this.src && b) {
93
		if (!this.loaded && !this.loading) {
94
			// load
95
			WebFXLoadTree.loadXmlDocument(this);
96
		}
97
	}
98
};
99
100
function WebFXLoadTreeItem(sText, sXmlSrc, oAction, eParent, sIcon, oIconAction, sOpenIcon) {
101
	WebFXTreeItem.call(this, sText, oAction, eParent, sIcon, oIconAction, sOpenIcon);
102
103
	// setup default property values
104
	this.src = sXmlSrc;
105
	this.loading = !sXmlSrc;
106
	this.loaded = !sXmlSrc;
107
	this.errorText = "";
108
109
	if (this.src) {
110
		/// add loading Item
111
		this._loadingItem = WebFXLoadTree.createLoadingItem();
112
		this.add(this._loadingItem);
113
114
		if (this.getExpanded()) {
115
			WebFXLoadTree.loadXmlDocument(this);
116
		}
117
	}
118
}
119
120
_p = WebFXLoadTreeItem.prototype = new WebFXTreeItem;
121
122
_p.setExpanded = function (b) {
123
	WebFXTreeItem.prototype.setExpanded.call(this, b);
124
125
	if (this.src && b) {
126
		if (!this.loaded && !this.loading) {
127
			// load
128
			WebFXLoadTree.loadXmlDocument(this);
129
		}
130
	}
131
};
132
133
// reloads the src file if already loaded
134
WebFXLoadTree.prototype.reload =
135
	_p.reload = function () {
136
		// if loading do nothing
137
		if (this.loaded) {
138
			var t = this.getTree();
139
			var expanded = this.getExpanded();
140
			var sr = t.getSuspendRedraw();
141
			t.setSuspendRedraw(true);
142
143
			// remove
144
			while (this.childNodes.length > 0) {
145
				this.remove(this.childNodes[this.childNodes.length - 1]);
146
			}
147
148
			this.loaded = false;
149
150
			this._loadingItem = WebFXLoadTree.createLoadingItem();
151
			this.add(this._loadingItem);
152
153
			if (expanded) {
154
				this.setExpanded(true);
155
			}
156
157
			t.setSuspendRedraw(sr);
158
			this.update();
159
		} else if (this.open && !this.loading) {
160
			WebFXLoadTree.loadXmlDocument(this);
161
		}
162
	};
163
164
165
WebFXLoadTree.prototype.setSrc =
166
	_p.setSrc = function (sSrc) {
167
		var oldSrc = this.src;
168
		if (sSrc == oldSrc) return;
169
170
		var expanded = this.getExpanded();
171
172
		// remove all
173
		this._callSuspended(function () {
174
			// remove
175
			while (this.childNodes.length > 0)
176
				this.remove(this.childNodes[this.childNodes.length - 1]);
177
		});
178
		this.update();
179
180
		this.loaded = false;
181
		this.loading = false;
182
		if (this._loadingItem) {
183
			this._loadingItem.dispose();
184
			this._loadingItem = null;
185
		}
186
		this.src = sSrc;
187
188
		if (sSrc) {
189
			this._loadingItem = WebFXLoadTree.createLoadingItem();
190
			this.add(this._loadingItem);
191
		}
192
193
		this.setExpanded(expanded);
194
	};
195
196
WebFXLoadTree.prototype.getSrc =
197
	_p.getSrc = function () {
198
		return this.src;
199
	};
200
201
WebFXLoadTree.prototype.dispose = function () {
202
	WebFXTree.prototype.dispose.call(this);
203
	if (this._xmlHttp) {
204
		if (this._xmlHttp.dispose) {
205
			this._xmlHttp.dispose();
206
		}
207
		try {
208
			this._xmlHttp.onreadystatechange = null;
209
			this._xmlHttp.abort();
210
		} catch (ex) {}
211
		this._xmlHttp = null;
212
	}
213
};
214
215
_p.dispose = function () {
216
	WebFXTreeItem.prototype.dispose.call(this);
217
	if (this._xmlHttp) {
218
		if (this._xmlHttp.dispose) {
219
			this._xmlHttp.dispose();
220
		}
221
		try {
222
			this._xmlHttp.onreadystatechange = null;
223
			this._xmlHttp.abort();
224
		} catch (ex) {}
225
		this._xmlHttp = null;
226
	}
227
};
228
229
230
// The path is divided by '/' and the item is identified by the text
231
WebFXLoadTree.prototype.openPath =
232
	_p.openPath = function (sPath, bSelect, bFocus) {
233
		// remove any old pending paths to open
234
		delete this._pathToOpen;
235
		//delete this._pathToOpenById;
236
		this._selectPathOnLoad = bSelect;
237
		this._focusPathOnLoad = bFocus;
238
239
		if (sPath == "") {
240
			if (bSelect) {
241
				this.select();
242
			}
243
			if (bFocus) {
244
				window.setTimeout("WebFXTreeAbstractNode._onTimeoutFocus(\"" + this.getId() + "\")", 10);
245
			}
246
			return;
247
		}
248
249
		var parts = sPath.split("/");
250
		var remainingPath = parts.slice(1).join("/");
251
252
		if (sPath.charAt(0) == "/") {
253
			this.getTree().openPath(remainingPath, bSelect, bFocus);
254
		} else {
255
			// open
256
			this.setExpanded(true);
257
			if (this.loaded) {
258
				parts = sPath.split("/");
259
				var ti = this.findChildByText(parts[0]);
260
				if (!ti) {
261
					throw "Could not find child node with text \"" + parts[0] + "\"";
262
				}
263
264
				ti.openPath(remainingPath, bSelect, bFocus);
265
			} else {
266
				this._pathToOpen = sPath;
267
			}
268
		}
269
	};
270
271
272
// Opera has some serious attribute problems. We need to use getAttribute
273
// for certain attributes
274
WebFXLoadTree._attrs = ["text", "src", "action", "id", "target"];
275
276
WebFXLoadTree.createItemFromElement = function (oNode) {
277
	var jsAttrs = {},
278
		jsAttrs2 = {},
279
		i,
280
		l;
281
282
283
	var children = oNode.childNodes;
284
	l = children.length;
285
	if (l > 0) {
286
		for (i = 0; i < l; i++) {
287
			if (children[i].tagName !== "tree" && children[i].innerHTML.length) {
288
				jsAttrs[children[i].tagName] = children[i].innerHTML.replace(/&#38;/g, "&").replace(/\&amp;/g, '&'); // replace for Safari fix for DOM Bug;
289
			}
290
		}
291
		//console.log('jsAttrs', jsAttrs);
292
	}
293
294
295
	/*l = oNode.attributes.length;
296
	for (i = 0; i < l; i++) {
297
		oNode.attributes[i].nodeValue = String(oNode.attributes[i].nodeValue).replace(/&#38;/g, "&"); // replace for Safari fix for DOM Bug
298
		if (oNode.attributes[i] == null) {
299
			continue;
300
		}
301
		jsAttrs2[oNode.attributes[i].nodeName] = oNode.attributes[i].nodeValue;
302
	}
303
304
	//console.log('jsAttrs2', jsAttrs2);
305
306
	var name, val;
307
	for (i = 0; i < WebFXLoadTree._attrs.length; i++) {
308
		name = WebFXLoadTree._attrs[i];
309
		value = oNode.getAttribute(name);
310
		if (value) {
311
			jsAttrs[name] = value;
312
		}
313
	}*/
314
315
	var action;
316
	if (jsAttrs.onaction) {
317
		action = new Function(jsAttrs.onaction);
318
	} else if (jsAttrs.action) {
319
		action = jsAttrs.action;
320
	}
321
	var jsNode = new WebFXLoadTreeItem(jsAttrs.html || "", jsAttrs.src, action,
322
		null, jsAttrs.icon, jsAttrs.iconaction, jsAttrs.openicon);
323
	if (jsAttrs.text) {
324
		jsNode.setText(jsAttrs.text);
325
	}
326
327
	if (jsAttrs.target) {
328
		jsNode.target = jsAttrs.target;
329
	}
330
	if (jsAttrs.id) {
331
		jsNode.setId(jsAttrs.id);
332
	}
333
	if (jsAttrs.tooltip) {
334
		jsNode.toolTip = jsAttrs.tooltip;
335
	}
336
	if (jsAttrs.expanded) {
337
		jsNode.setExpanded(jsAttrs.expanded != "false");
338
	}
339
	if (jsAttrs.onload) {
340
		jsNode.onload = new Function(jsAttrs.onload);
341
	}
342
	if (jsAttrs.onerror) {
343
		jsNode.onerror = new Function(jsAttrs.onerror);
344
	}
345
346
	jsNode.attributes = jsAttrs;
347
348
	// go through childNodes
349
	var cs = oNode.childNodes;
350
	l = cs.length;
351
	for (i = 0; i < l; i++) {
352
		if (cs[i].tagName == "tree") {
353
			jsNode.add(WebFXLoadTree.createItemFromElement(cs[i]));
354
		}
355
	}
356
357
	return jsNode;
358
};
359
360
WebFXLoadTree.loadXmlDocument = function (jsNode) {
361
	if (jsNode.loading || jsNode.loaded) {
362
		return;
363
	}
364
	jsNode.loading = true;
365
	var id = jsNode.getId();
366
	jsNode._xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest : new window.ActiveXObject("Microsoft.XmlHttp");
367
	jsNode._xmlHttp.open("GET", jsNode.src, true); // async
368
	jsNode._xmlHttp.onreadystatechange = new Function("WebFXLoadTree._onload(\"" + id + "\")");
369
370
	// call in new thread to allow ui to update
371
	window.setTimeout("WebFXLoadTree._ontimeout(\"" + id + "\")", 10);
372
};
373
374
WebFXLoadTree._onload = function (sId) {
375
	var jsNode = webFXTreeHandler.all[sId];
376
	if (jsNode._xmlHttp.readyState == 4) {
377
		WebFXLoadTree.documentLoaded(jsNode);
378
		webFXLoadTreeQueue.remove(jsNode);
379
		if (jsNode._xmlHttp.dispose)
380
			jsNode._xmlHttp.dispose();
381
		jsNode._xmlHttp = null;
382
	}
383
};
384
385
WebFXLoadTree._ontimeout = function (sId) {
386
	var jsNode = webFXTreeHandler.all[sId];
387
	webFXLoadTreeQueue.add(jsNode);
388
};
389
390
391
// Inserts an xml document as a subtree to the provided node
392
WebFXLoadTree.documentLoaded = function (jsNode) {
393
	if (jsNode.loaded) {
394
		return;
395
	}
396
397
	jsNode.errorText = "";
398
	jsNode.loaded = true;
399
	jsNode.loading = false;
400
401
	var t = jsNode.getTree();
402
	var oldSuspend = t.getSuspendRedraw();
403
	t.setSuspendRedraw(true);
404
405
	var doc = jsNode._xmlHttp.responseXML;
406
407
	// check that the load of the xml file went well
408
	if (!doc || doc.parserError && doc.parseError.errorCode != 0 || !doc.documentElement) {
409
		if (!doc || doc.parseError.errorCode == 0) {
410
			jsNode.errorText = webFXTreeConfig.errorLoadingText + " " + jsNode.src + " (" + jsNode._xmlHttp.status + ": " + jsNode._xmlHttp.statusText + ")";
411
		} else {
412
			jsNode.errorText = webFXTreeConfig.errorLoadingText + " " + jsNode.src + " (" + doc.parseError.reason + ")";
413
		}
414
	} else {
415
		// there is one extra level of tree elements
416
		var root = doc.documentElement;
417
418
		// loop through all tree children
419
		var count = 0;
420
		var cs = root.childNodes;
421
		var l = cs.length;
422
		var newNode;
423
		for (var i = 0; i < l; i++) {
424
			if (cs[i].tagName == "tree") {
425
				newNode = WebFXLoadTree.createItemFromElement(cs[i]);
426
				jsNode.add(newNode);
427
				count++;
428
			}
429
		}
430
431
		if (count == 1 && newNode.childNodes.length) {
432
			var parent = jsNode.parentNode;
433
			newNode.setExpanded(true);
434
		}
435
		// if no children we got an error
436
		if (count == 0) {
437
			jsNode.errorText = webFXTreeConfig.errorLoadingText + " " + jsNode.src + " (???)";
438
		}
439
	}
440
441
	if (jsNode.errorText != "") {
442
		jsNode._loadingItem.icon = webFXTreeConfig.errorIcon;
443
		jsNode._loadingItem.text = jsNode.errorText;
444
		jsNode._loadingItem.action = WebFXLoadTree._reloadParent;
445
		jsNode._loadingItem.toolTip = webFXTreeConfig.reloadText;
446
447
		t.setSuspendRedraw(oldSuspend);
448
449
		jsNode._loadingItem.update();
450
451
		if (typeof jsNode.onerror == "function") {
452
			jsNode.onerror();
453
		}
454
	} else {
455
		// remove dummy
456
		if (jsNode._loadingItem != null) {
457
			jsNode.remove(jsNode._loadingItem);
458
		}
459
460
		if (jsNode._pathToOpen) {
461
			jsNode.openPath(jsNode._pathToOpen, jsNode._selectPathOnLoad, jsNode._focusPathOnLoad);
462
		}
463
464
		t.setSuspendRedraw(oldSuspend);
465
		jsNode.update();
466
		if (typeof jsNode.onload == "function") {
467
			jsNode.onload();
468
		}
469
	}
470
};
471
472
WebFXLoadTree._reloadParent = function () {
473
	this.getParent().reload();
474
};
475
476
477
var webFXLoadTreeQueue = {
478
	_nodes: [],
479
	_ie: /msie/i.test(navigator.userAgent),
480
	_opera: /opera/i.test(navigator.userAgent),
481
482
	add: function (jsNode) {
483
		if (this._ie || this._opera) {
484
			this._nodes.push(jsNode);
485
			if (this._nodes.length == 1) {
486
				this._send();
487
			}
488
		} else {
489
			jsNode._xmlHttp.send(null);
490
		}
491
	},
492
493
	remove: function (jsNode) {
494
		if (this._ie || this._opera) {
495
			arrayHelper.remove(this._nodes, jsNode);
496
			if (this._nodes.length > 0) {
497
				this._send();
498
			}
499
		}
500
	},
501
502
	// IE only
503
	_send: function () {
504
		var id = this._nodes[0].getId();
505
		var jsNode = webFXTreeHandler.all[id];
506
		if (!jsNode) {
507
			return;
508
		}
509
		// if no _xmlHttp then remove it
510
		if (!jsNode._xmlHttp) {
511
			this.remove(jsNode);
512
		} else {
513
			jsNode._xmlHttp.send(null);
514
		}
515
	}
516
};